define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    if(Config.admin_id == '' || Config.admin_id == null){
        Config.admin_id = 0;
    }
    if(Config.timedate == '' || Config.timedate == null){
        Config.timedate = 0;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'feditionreportmonth/index/?admin_id=' + Config.admin_id+'&timedate=' + Config.timedate,
                    add_url: 'feditionreportmonth/add',
                    edit_url: 'feditionreportmonth/edit',
                    del_url: 'feditionreportmonth/del',
                    multi_url: 'feditionreportmonth/multi',
                    import_url: 'feditionreportmonth/import',
                    table: 'fedition_reportmonth',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'timedate',
                search:false,
                searchFormVisible: true,
                //启用固定列
                fixedColumns: true,
                fixedNumber: 0,
                // fixedRightNumber: 3,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'timedate', title: __('Timedate'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, datetimeFormat:"YYYY-MM"},
                        //{field: 'admin_id', title: __('店铺管理者'),visible: false,addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        //{field: 'admin_name', title: __('店铺管理者'), operate: false},
                        {field: 'accounturl', title: __('Accounturl'),visible: false,addClass: "selectpage", extend: "data-source='feditionaccounturl/index' data-field='name'"},
                        {field: 'accounturl_name', title: __('Accounturl'), operate: false},
                        {field: 'material', title: __('采购成本($)'), operate:'BETWEEN',sortable:true},
                        {field: 'freightmj', title: __('物流成本($)'), operate:'BETWEEN',sortable:true},
                        {field: 'advertisingz', title: __('广告成本($)'), operate:'BETWEEN',sortable:true},
                        {field: 'zcost', title: __('总成本($)'), operate:'BETWEEN',sortable:true},

                        {field: 'total_price', title: __('原销售额')},
                        {field: 'total_tip_received', title: __('原小费')},
                        {field: 'saleszy', title: __('原总销售额')},
                        {field: 'salesz', title: __('现总销售额($)')},
                        {field: 'salesz_discount', title: __('总销售额($)(95%)'), operate:'BETWEEN',sortable:true},
                        {field: 'gross_profit', title: __('总毛利($)'), operate:'BETWEEN',sortable:true},
                        // {field: 'info', title: __('备注'), operate:false},
                        {field: 'zorder', title: __('总订单'), operate: false,formatter: Controller.api.formatter.zorder,sortable:true},
                        {field: 'freight_known', title: __('运费成本连通'), operate: false,formatter: Controller.api.formatter.freight_known},
                        {field: 'freight_unknown', title: __('运费成本未知'), operate: false,formatter: Controller.api.formatter.freight_unknown},
                        {field: 'material_known', title: __('采购成本连通'), operate: false,formatter: Controller.api.formatter.material_known},
                        {field: 'material_unknown', title: __('采购成本未知'), operate: false,formatter: Controller.api.formatter.material_unknown},
                        //{
                        //    field: 'buttons',
                        //    width: "120px",
                        //    title: __('采购成本'),
                        //    table: table,
                        //    events: Table.api.events.operate,
                        //    operate: false,
                        //    buttons: [
                        //        {
                        //            name: 'detail',
                        //            text: __('采购成本详情'),
                        //            title: __('采购成本详情'),
                        //            extend: 'data-area=\'["90%","90%"]\'',
                        //            classname: 'btn btn-sm btn-warning btn-dialog',
                        //            icon: 'fa fa-spotify',
                        //            url: 'feditionproductmaterial/index/?accounturl={accounturl}&timedate={timedate}'
                        //        },
                        //    ],
                        //    formatter: Table.api.formatter.buttons
                        //},
                        //{
                        //    field: 'buttons',
                        //    width: "120px",
                        //    title: __('广告成本'),
                        //    table: table,
                        //    events: Table.api.events.operate,
                        //    operate: false,
                        //    buttons: [
                        //        {
                        //            name: 'detail',
                        //            text: __('广告成本详情'),
                        //            title: __('广告成本详情'),
                        //            extend: 'data-area=\'["90%","90%"]\'',
                        //            classname: 'btn btn-sm btn-info btn-dialog',
                        //            icon: 'fa fa-spotify',
                        //            url: 'feditionreportone/index/?accounturl={accounturl}&timedate={timedate}'
                        //        },
                        //    ],
                        //    formatter: Table.api.formatter.buttons
                        //},
                        //{
                        //    field: 'buttons',
                        //    width: "120px",
                        //    title: __('查看详情'),
                        //    table: table,
                        //    events: Table.api.events.operate,
                        //    operate: false,
                        //    buttons: [
                        //        {
                        //            name: 'detail',
                        //            text: __('销售额详情'),
                        //            title: __('销售额详情'),
                        //            extend: 'data-area=\'["90%","90%"]\'',
                        //            classname: 'btn btn-sm btn-success btn-dialog',
                        //            icon: 'fa fa-spotify',
                        //            url: 'feditionstatisticsshopify/index/?accounturl={accounturl}&timedate={timedate}'
                        //        },
                        //    ],
                        //    formatter: Table.api.formatter.buttons
                        //},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('更新业绩'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('点击更新业绩'),
                                    title: __('点击更新业绩'),
                                    classname: 'btn btn-sm btn-warning btn-magic btn-ajax',
                                    icon: 'fa fa-leaf',
                                    confirm: '等待时间约一分钟，确认开始更新吗？',
                                    url: 'feditionreportmonth/edit_status',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        //刷新父级页面
                                        // window.parent.location.reload();
                                        //刷新当前页面
                                        table.bootstrapTable('refresh', {});
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
// 当表格数据加载完成时
            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                $("#zcosts").text(data.Statistics.zcosts);
                $("#saleszs").text(data.Statistics.saleszs);
                // $("#advertisingz").text(data.Statistics.advertisingz);
                $("#gross_profits").text(data.Statistics.gross_profits);
            });

            $(document).on("click", ".btn-zorder", function () {
                var ids = $(this).data("ids");
                Fast.api.open("feditionordershopifyc/index?monthtype=1&monthids=" + ids, __('查看订单列表'));
            });
            $(document).on("click", ".btn-freight_unknown", function () {
                var ids = $(this).data("ids");
                Fast.api.open("feditionordershopifyc/index?monthtype=3&monthids=" + ids, __('查看订单列表'));
            });
            $(document).on("click", ".btn-material_unknown", function () {
                var ids = $(this).data("ids");
                console.log(ids);
                Fast.api.open("feditionordershopifyc/index?monthtype=5&monthids=" + ids, __('查看订单列表'));
            });
            $(document).on("click", ".btn-freight_known", function () {
                var ids = $(this).data("ids");
                Fast.api.open("feditionordershopifyc/index?monthtype=2&monthids=" + ids, __('查看订单列表'));
            });
            $(document).on("click", ".btn-material_known", function () {
                var ids = $(this).data("ids");
                Fast.api.open("feditionordershopifyc/index?monthtype=4&monthids=" + ids, __('查看订单列表'));
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                zorder: function (value, row, index) {
                    return '<a href="javascript:;" data-ids="' + row.id + '" class="btn btn-zorder" title="查看总订单:' + value+ '条">' + value + '</a>';
                },
                freight_unknown: function (value, row, index) {
                    return '<a href="javascript:;" data-ids="' + row.id + '" class="btn btn-freight_unknown" title="查看总订单:' + value+ '条">' + value + '</a>';
                },
                material_unknown: function (value, row, index) {
                    return '<a href="javascript:;" data-ids="' + row.id + '" class="btn btn-material_unknown" title="查看总订单:' + value+ '条">' + value + '</a>';
                },
                freight_known: function (value, row, index) {
                    return '<a href="javascript:;" data-ids="' + row.id + '" class="btn btn-freight_known" title="查看总订单:' + value+ '条">' + value + '</a>';
                },
                material_known: function (value, row, index) {
                    return '<a href="javascript:;" data-ids="' + row.id + '" class="btn btn-material_known" title="查看总订单:' + value+ '条">' + value + '</a>';
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});